<?php
/**
 * Yotaku shop - yshop
 * Copyright (c) 2016 http://www.yotaku.cn All rights reserved.
 * Author: Creatint <creatint@163.com> <http://creatint.yotaku.cn>
 */

namespace Home\Controller;
class PayController extends  HomeController{

    public function buy() {
        $order_id = I("get.order_id",0);
        if(!$order_id){
            echo ('订单不存在');
            return;
        }
        //获取订单商品
        $Order = D("Order");
        $order_data = $Order->getDetail($order_id);
        if(!$order_data){
            echo $Order->getError();
            return;
        }
        $order_data['cartdata'] = json_decode($order_data['cartdata'],true);
        trace($order_data,'order');
        $this->assign("order",$order_data);


        //C('URL_MODEL',2);
        Vendor('WxpayAPI.WxPayJsApiPay');
        Vendor('WxpayAPI.log');
        //初始化日志
        $logHandler= new \CLogFileHandler("logs/".date('Y-m-d').'.log');
        $log = \Log::Init($logHandler, 15);

        //打印输出数组信息
        /*
        function printf_info($data)
        {
            foreach($data as $key=>$value){
                echo "<font color='#00ff55;'>$key</font> : $value <br/>";
            }
        }
		 */
		
        //①、获取用户openid
        $tools = new \JsApiPay();
        $openId = $tools->GetOpenid();
        //②、统一下单
        $input = new \WxPayUnifiedOrder();
        $input->SetBody("商品支付"); //商品描述
        $input->SetAttach($order_data['id']); // 附加数据
        $input->SetOut_trade_no(\WxPayConfig::MCHID.date("YmdHis"));
        $input->SetTotal_fee($order_data['total_price']*100); // 金额
        $input->SetTime_start(date("YmdHis")); // 交易起始时间
        $input->SetTime_expire(date("YmdHis", time() + 600)); // 交易结束时间
        $input->SetGoods_tag("test"); // 商品标记
        $input->SetNotify_url("http://shengxueshan.huiyouzan.com/index.php/Pay/notify"); // 通知地址
        $input->SetTrade_type("JSAPI");
        $input->SetOpenid($openId); // 用户openid
        $order = \WxPayApi::unifiedOrder($input);
        //echo '<font color="#f00"><b>统一下单支付单信息</b></font><br/>';
        //printf_info($order);
        $jsApiParameters = $tools->GetJsApiParameters($order);
        $this->assign('jsApiParameters',$jsApiParameters);
        //获取共享收货地址js函数参数
        //$editAddress = $tools->GetEditAddressParameters();


        $this->display();
    }

    public function notify() {
        file_put_contents('logs/pay.txt',"ccc");
        //echo file_get_contents('logs/txt.txt')
        Vendor('WxpayAPI.notify');
        \Log::DEBUG("begin notify");
        $notify = new \PayNotifyCallBack();
        $notify->Handle(false);
        $json = json_encode($notify->attr);
        file_put_contents('logs/pay.txt',$json);
//        $res = $notify->notifyprocess();
//        $json = json_encode($res);
//        file_put_contents('logs/pay.txt',$json);
        if($notify->attr['status']===true){
            //TODO::已支付，记录data
            $Order = D("Order");
            $order = array(
                'id'=>$notify->attr['data']['attach'],
                'transaction_id'=>$notify->attr['data']['transaction_id'],
                'time_end'=>time(),
                'pay_status'=>1,

            );
            if($Order->update($order)){
                $this->success('支付成功',U('Order/success'));
            }
        }
        $this->error('支付失败',U('Center/order'));
    }
}
